package org.interactive.spring.intro.advice;

import org.interactive.spring.intro.exceptions.UserNotFoundException;
import org.interactive.spring.intro.exceptions.ValidationException;
import org.interactive.spring.intro.transport.GenericResponse;
import org.interactive.spring.intro.transport.UsersResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * Class used to hold exception handlers
 *
 * @author bogdan.solga
 *
 * Date: 05.06.2013, time: 09:56
 */
@ControllerAdvice
public class ExceptionHandlers {

    @ExceptionHandler(UserNotFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public @ResponseBody UsersResponse userNotFoundException(UserNotFoundException e) {
        UsersResponse response = new UsersResponse();

        response.setErrorMessage(e.getMessage());

        return response;
    }

    @ExceptionHandler(ValidationException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public @ResponseBody GenericResponse validationException(ValidationException e) {
        GenericResponse response = new GenericResponse();

        response.setErrorMessage(e.getMessage());

        return response;
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public @ResponseBody String internalServerError(Exception e) {
        e.printStackTrace();
        return "Internal server error";
    }
}
